<template>
  <div class="ldm-page">
    <!-- 顶部：返回 | 新增/编辑 -->
    <div class="page-bar">
      <a class="back" @click="goBack">
        <LeftOutlined />
        <span class="text">返回</span>
      </a>
      <span class="vsep">|</span>
      <span class="title">{{ isEdit ? "编辑" : "新增" }}</span>
    </div>

    <!-- 新父级 -->
    <div class="content-area">
      <!-- 表单 -->
      <div class="body">
        <a-form
          :model="form"
          :label-col="{ style: 'width: 96px;' }"
          :wrapper-col="{ style: 'max-width: 520px;' }"
        >
          <a-form-item label="名称：" required>
            <a-input v-model:value="form.link_name" placeholder="请输入名称" />
          </a-form-item>

          <a-form-item label="链接地址：" required>
            <a-input
              v-model:value="form.url"
              placeholder="请输入要删除的链接 URL"
            />
          </a-form-item>

          <a-form-item label="备注：">
            <a-input v-model:value="form.memo" placeholder="可选" />
          </a-form-item>

          <a-form-item label="状态：">
            <a-radio-group v-model:value="form.status">
              <a-radio value="enabled">启用</a-radio>
              <a-radio value="disabled">停用</a-radio>
            </a-radio-group>
          </a-form-item>
        </a-form>
      </div>

      <!-- 底部 -->
      <div class="page-footer">
        <div class="footer-inner">
          <a-button type="link" class="btn-cancel" @click="goBack"
            >取消</a-button
          >
          <span class="sep">|</span>
          <a-button
            type="link"
            class="btn-ok"
            :loading="submitting"
            @click="onSubmit"
            >确定</a-button
          >
        </div>
      </div>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { reactive, ref, onMounted } from "vue";
import { useRoute, useRouter } from "vue-router";
import { message } from "ant-design-vue";
import { LeftOutlined } from "@ant-design/icons-vue";
import {
  getLinkDeletionById,
  createLinkDeletion,
  updateLinkDeletion,
  type LinkDeletionItem,
} from "@/api/modules/linkDeletion.api";

const router = useRouter();
const route = useRoute();
const TAB_KEY = "linkDeletionManagement";

const isEdit = route.query.mode === "edit";
const id = route.query.id ? String(route.query.id) : "";

const form = reactive<{
  link_name: string;
  url: string;
  memo?: string;
  status: "enabled" | "disabled";
}>({
  link_name: "",
  url: "",
  memo: "",
  status: "enabled",
});

const submitting = ref(false);

async function init() {
  if (!isEdit) return;
  if (route.query.row) {
    try {
      const row = JSON.parse(decodeURIComponent(String(route.query.row)));
      form.link_name = row.link_name || "";
      form.url = row.url || "";
      form.memo = row.memo || "";
      form.status = row.status || "enabled";
      return;
    } catch {
      /* ignore */
    }
  }
  try {
    const data = await getLinkDeletionById(id);
    form.link_name = data?.link_name || "";
    form.url = data?.url || "";
    form.memo = data?.memo || "";
    form.status = data?.status === "published" ? "enabled" : "disabled";
  } catch (e: any) {
    message.error(e?.message || "加载失败");
  }
}
onMounted(init);

function backTarget() {
  const backTo = route.query.backTo;
  const from = route.query.from as string | undefined;
  if (backTo === "tab" && from) {
    return { path: from, query: { tab: TAB_KEY } };
  }
  return { path: "/linkDeletionManagement" };
}

function goBack() {
  router.replace(backTarget());
}

async function onSubmit() {
  if (!form.link_name?.trim()) {
    message.warning("请填写名称");
    return;
  }
  if (!form.url?.trim()) {
    message.warning("请填写链接地址");
    return;
  }
  submitting.value = true;
  try {
    const payload: Partial<LinkDeletionItem> = {
      link_name: form.link_name.trim(),
      url: form.url.trim(),
      memo: form.memo?.trim() || undefined,
      status: form.status === "enabled" ? "published" : "draft",
    };
    if (isEdit) {
      await updateLinkDeletion(id, payload);
    } else {
      await createLinkDeletion(payload as any);
    }
    message.success("保存成功");
    goBack();
  } catch (e: any) {
    message.error(e?.message || "保存失败");
  } finally {
    submitting.value = false;
  }
}
</script>

<style lang="less" scoped>
@pad-x: 24px;
@wrap-max: 520px;
@label-w: 96px; 
@safe-bottom: env(safe-area-inset-bottom);

.ldm-page {
  background: #fff;
  border: 1px solid #eaeaea;
  border-radius: 6px;
  min-height: 100vh;
  min-height: 100dvh;
  height: 100%;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  box-sizing: border-box;
}

/* 顶部栏 */
.page-bar {
  display: flex;
  align-items: center;
  padding: 14px @pad-x;
  flex: 0 0 auto;

  .back {
    display: inline-flex;
    align-items: center;
    color: #81889c;
    font-size: 14px;
    cursor: pointer;
    text-decoration: none;

    .anticon {
      margin-right: 6px;
    }
    .text {
      line-height: 1;
    }
  }

  .vsep {
    margin: 0 12px;
    color: #bfbfbf;
  }

  .title {
    font-size: 16px;
    font-weight: 600;
    color: #222;
  }
}

/* 新父级滚动容器 */
.content-area {
  flex: 1 1 auto;
  min-height: 0;
  display: flex;
  flex-direction: column;
  overflow-y: auto;
  overflow-x: hidden;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;

  /* 自定义滚动条 */
  &::-webkit-scrollbar {
    width: 10px;
    height: 10px;
  }
  &::-webkit-scrollbar-thumb {
    background: rgba(0, 0, 0, 0.15);
    border-radius: 6px;
    border: 2px solid transparent;
    background-clip: padding-box;
  }
  &::-webkit-scrollbar-thumb:hover {
    background: rgba(0, 0, 0, 0.25);
  }
  &::-webkit-scrollbar-track {
    background: transparent;
  }
  scrollbar-width: thin;
  scrollbar-color: rgba(0, 0, 0, 0.25) transparent;
  scrollbar-gutter: stable;
  padding-bottom: calc(16px + @safe-bottom);
}

/* 表单本体 */
.body {
  padding: 20px @pad-x 0 85px;

  :deep(.ant-form-item) {
    margin-bottom: 20px;
  }
  :deep(.ant-form-item-label > label) {
    font-size: 14px;
  }
}

/* 底部操作栏 */
.page-footer {
  position: static;
  margin-top: 16px;
  padding: 12px @pad-x calc(12px + @safe-bottom) @pad-x;
  background: #fff;
  border-top: 1px solid #f0f0f0;
  box-shadow: 0 -2px 8px rgba(0, 0, 0, 0.03);

  .footer-inner {
    max-width: @wrap-max;
    margin: 0 auto;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 12px;
    flex-wrap: wrap;
    width: 100%;
    padding-left: @label-w; 
  }

  .sep {
    color: #e5e7eb;
    user-select: none;
    line-height: 24px;
    height: 24px;
    display: inline-flex;
    align-items: center;
    margin: 0 2px;
  }

  .btn-cancel {
    height: 36px;
    padding: 0 16px;
    border-radius: 8px;
    background: #fafafa !important;
    border: 1px solid #e8e8e8 !important;
    color: #666 !important;
    transition: all 0.2s ease;
  }
  .btn-cancel:hover {
    background: #fff !important;
    color: #595959 !important;
    border-color: #d9d9d9 !important;
  }

  .btn-ok {
    height: 36px;
    padding: 0 20px;
    border-radius: 8px;
    font-weight: 600;
    // min-width: 96px;
    background: #1677ff !important;
    border: 1px solid #1677ff !important;
    color: #fff !important;
    transition: box-shadow 0.2s ease, filter 0.2s ease;
  }
  .btn-ok:hover {
    filter: brightness(1.02);
    box-shadow: 0 2px 8px rgba(22, 119, 255, 0.2);
  }
  .btn-ok[disabled],
  .btn-ok[disabled]:hover {
    background: #69b1ff !important;
    border-color: #69b1ff !important;
    box-shadow: none;
    color: #fff !important;
    opacity: 0.85;
  }
}

/* 小屏优化 */
@media (max-width: 576px) {
  .page-bar {
    padding: 12px 12px;
  }
  .body {
    padding: 16px 12px 0 12px;
  }
  .page-footer {
    padding-left: 12px;
    padding-right: 12px;
  }
}

.divider,
.top-divider,
.bottom-divider {
  display: none;
}
</style>
